package io.renren.modules.school.service.impl;

import io.renren.common.utils.R;
import io.renren.common.utils.ReadExcel;
import io.renren.modules.school.entity.PrefessionLowScoreEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.school.dao.ProfessionDao;
import io.renren.modules.school.entity.ProfessionEntity;
import io.renren.modules.school.service.ProfessionService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.ws.Action;


@Service("professionService")
public class ProfessionServiceImpl extends ServiceImpl<ProfessionDao, ProfessionEntity> implements ProfessionService {

    @Autowired
    ProfessionDao professionDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String schoolId = (String)params.get("schoolId");
        Page<ProfessionEntity> page = this.selectPage(
                new Query<ProfessionEntity>(params).getPage(),
                new EntityWrapper<ProfessionEntity>().eq("school_id",schoolId)
        );

        return new PageUtils(page);
    }

    @Override
    public void insertProfessionYear(PrefessionLowScoreEntity profession) {
        professionDao.insertProfessionYear(profession);
    }

    @Override
    public PrefessionLowScoreEntity getOnePLS(PrefessionLowScoreEntity profession) {
        return professionDao.getOnePLS(profession);
    }

    @Override
    public List getProfessions(Map<String, Object> params) {
        String wenlike= (String) params.get("wenlike");
        if(wenlike.equals("理科")){
            params.put("predictor","like_score");
            params.put("wenli","文科");
        }else{
            params.put("predictor","wenke_score");
            params.put("wenli","理科");
        }

        return professionDao.getProfessions(params);
    }

    @Override
    @Transactional
    public R saveMall(MultipartFile file) {
        int a=0,b=0,c=0,o=0;
        List list = new ArrayList();
        try {
            ReadExcel excel = new ReadExcel();
            List<String[]> data = excel.readExcel(file);
            a = data.size();  //行数 除了第一行的行数

            for(o = 0;o<a;o++){
                ProfessionEntity professionEntity = new ProfessionEntity();
                professionEntity.setCode(data.get(o)[0]);
                professionEntity.setName(data.get(o)[1]);
                professionEntity.setWenlike(data.get(o)[2]);
                professionEntity.setSchoolId(Integer.parseInt(data.get(o)[3]));
                professionEntity.setSchoolName(data.get(o)[4]);
                professionEntity.setType(data.get(o)[5]);
                professionEntity.setSchoolCode(data.get(o)[6]);
                professionEntity.setWenkeScore(null);
                professionEntity.setLikeScore(null);
                professionEntity.setLevel(data.get(o)[9]);
                list.add(professionEntity);
            }
            professionDao.savaMall(list);
               return R.ok();

        }catch(NumberFormatException ne){
            ne.printStackTrace();
            return R.error(102,"您的第"+(o+1)+"行数据数字出现问题，请重新上传");
        }
        catch(Exception e){
            e.printStackTrace();
            return R.error(102,"您的第"+(o+1)+"行数据有问题，请重新上传");
        }
    }

}
